package com.tool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tool.util.TemplateUtil;
import com.tool.views.Column;
import com.tool.views.TableInfo;
import com.tool.views.TableView;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class YShop {

	private final static Logger LOGGER = LoggerFactory.getLogger(YShop.class);

	public static void main(String[] args) throws Exception {

		Template vueTpl = TemplateUtil.getTemplate("vue.ftl");
		Template svTpl = TemplateUtil.getTemplate("service.ftl");
		Template toolTpl = TemplateUtil.getTemplate("convert.ftl");
		Template queryTpl = TemplateUtil.getTemplate("query.ftl");
		Template svImplTpl = TemplateUtil.getTemplate("serviceImpl.ftl");
		Template jsTpl = TemplateUtil.getTemplate("api.js.ftl");
		Template acTpl = TemplateUtil.getTemplate("controller.ftl");

		TableView admin_resources1 = new TableView();
		admin_resources1.setTableName("alipay_config");
		admin_resources1.setAlias("alipay");
		admin_resources1.setDescription("阿里支付配置");
//		admin_resources1.setForms(
//				Sets.newHashSet("airport_code", "device_sn", "device_type", "device_model", "user", "airport_id",
//						"airline", "longitude", "latitude", "province", "zone", "status_code", "remarks", "status"));
//		admin_resources1
//				.setSearchs(Sets.newHashSet("airport_code", "device_sn", "device_type", "device_model", "user"));
//		admin_resources1.setIngores(Sets.newHashSet("ctime", "deleted", "company_id", "utime", "uuid"));

		TableView admin_resources0 = new TableView();
		admin_resources0.setTableName("oss_content");
		admin_resources0.setAlias("oss");
		admin_resources0.setDescription("文件管理");
//		admin_resources0.setForms(Sets.newHashSet("device_id", "part_name", "part_code", "remarks", "status_code"));
//		admin_resources0.setSearchs(Sets.newHashSet("part_name", "part_code"));
//		admin_resources0.setIngores(Sets.newHashSet("ctime", "deleted", "uuid", "company_id", "utime"));

		TableView admin_resources2 = new TableView();
		admin_resources2.setTableName("picture");
		admin_resources2.setAlias("picture");
		admin_resources2.setDescription("图片管理");
//		admin_resources2.setForms(Sets.newHashSet("luggage_sn", "laggage_weight", "laggage_size", "passenger_name",
//				"passenger_id", "flight_no", "checkin_time", "checkin_type", "device_sn", "device_loc", "security_res",
//				"", "", "", "", ""));
//		admin_resources2.setSearchs(Sets.newHashSet("luggage_sn", "laggage_weight", "passenger_name", "flight_no",
//				"device_sn", "checkin_type", "device_loc"));
//		admin_resources2.setIngores(Sets.newHashSet("ctime", "deleted", "uuid", "company_id", "utime"));

		TableView admin_resources3 = new TableView();
		admin_resources3.setTableName("yx_express");
		admin_resources3.setAlias("express");
		admin_resources3.setDescription("快递信息");
//		admin_resources3.setForms(Sets.newHashSet("file_name", "file_type", "file_size", "oss_type", "status"));
//		admin_resources3.setSearchs(Sets.newHashSet("file_name", "file_type"));
//		admin_resources3.setIngores(Sets.newHashSet("ctime", "deleted", "uuid", "company_id", "utime"));

		List<TableView> tables = Lists.newArrayList(admin_resources0, admin_resources1, admin_resources2,
				admin_resources3);

		TableView bargain = new TableView();
		bargain.setTableName("yx_store_bargain");
		bargain.setAlias("bargain");
		bargain.setDescription("砍价表");
		tables.add(bargain);

		TableView bargainUser = new TableView();
		bargainUser.setTableName("yx_store_bargain_user");
		bargainUser.setAlias("bargainUser");
		bargainUser.setDescription("用户参与砍价表");
		tables.add(bargainUser);

		TableView bargainUserHelp = new TableView();
		bargainUserHelp.setTableName("yx_store_bargain_user_help");
		bargainUserHelp.setAlias("bargainUserHelp");
		bargainUserHelp.setDescription("砍价用户帮助表");
		tables.add(bargainUserHelp);

		TableView cart = new TableView();
		cart.setTableName("yx_store_cart");
		cart.setAlias("cart");
		cart.setDescription("购物车");
		tables.add(cart);

		TableView category = new TableView();
		category.setTableName("yx_store_category");
		category.setAlias("category");
		category.setDescription("商品分类表");
		tables.add(category);

		TableView combination = new TableView();
		combination.setTableName("yx_store_combination");
		combination.setAlias("combination");
		combination.setDescription("拼团产品表");
		tables.add(combination);

		TableView coupon = new TableView();
		coupon.setTableName("yx_store_coupon");
		coupon.setAlias("coupon");
		coupon.setDescription("优惠券表");
		tables.add(coupon);

		TableView couponIssue = new TableView();
		couponIssue.setTableName("yx_store_coupon_issue");
		couponIssue.setAlias("couponIssue");
		couponIssue.setDescription("优惠券前台领取表");
		tables.add(couponIssue);

		TableView couponIssueUser = new TableView();
		couponIssueUser.setTableName("yx_store_coupon_issue_user");
		couponIssueUser.setAlias("couponIssueUser");
		couponIssueUser.setDescription("优惠券前台用户领取记录表");
		tables.add(couponIssueUser);

		TableView couponUser = new TableView();
		couponUser.setTableName("yx_store_coupon_user");
		couponUser.setAlias("couponUser");
		couponUser.setDescription("优惠券发放记录表");
		tables.add(couponUser);

		TableView order = new TableView();
		order.setTableName("yx_store_order");
		order.setAlias("order");
		order.setDescription("订单表");
		tables.add(order);

		TableView order_cart_info = new TableView();
		order_cart_info.setTableName("yx_store_order_cart_info");
		order_cart_info.setAlias("orderCartInfo");
		order_cart_info.setDescription("订单购物详情表");
		tables.add(order_cart_info);

		TableView yx_store_order_status = new TableView();
		yx_store_order_status.setTableName("yx_store_order_status");
		yx_store_order_status.setAlias("orderStatus");
		yx_store_order_status.setDescription("订单操作记录表");
		tables.add(yx_store_order_status);

		TableView yx_store_pink = new TableView();
		yx_store_pink.setTableName("yx_store_pink");
		yx_store_pink.setAlias("pink");
		yx_store_pink.setDescription("拼团表");
		tables.add(yx_store_pink);

		TableView yx_store_product = new TableView();
		yx_store_product.setTableName("yx_store_product");
		yx_store_product.setAlias("product");
		yx_store_product.setDescription("商品表");
		tables.add(yx_store_product);

		TableView yx_store_product_attr = new TableView();
		yx_store_product_attr.setTableName("yx_store_product_attr");
		yx_store_product_attr.setAlias("productAttr");
		yx_store_product_attr.setDescription("商品属性表");
		tables.add(yx_store_product_attr);

		TableView yx_store_product_attr_result = new TableView();
		yx_store_product_attr_result.setTableName("yx_store_product_attr_result");
		yx_store_product_attr_result.setAlias("productAttrResult");
		yx_store_product_attr_result.setDescription("商品属性详情表");
		tables.add(yx_store_product_attr_result);

		TableView yx_store_product_attr_value = new TableView();
		yx_store_product_attr_value.setTableName("yx_store_product_attr_value");
		yx_store_product_attr_value.setAlias("productAttrValue");
		yx_store_product_attr_value.setDescription("商品属性值表");
		tables.add(yx_store_product_attr_value);

		TableView yx_store_product_relation = new TableView();
		yx_store_product_relation.setTableName("yx_store_product_relation");
		yx_store_product_relation.setAlias("productRelation");
		yx_store_product_relation.setDescription("商品点赞和收藏表");
		tables.add(yx_store_product_relation);

		TableView yx_store_product_reply = new TableView();
		yx_store_product_reply.setTableName("yx_store_product_reply");
		yx_store_product_reply.setAlias("productReply");
		yx_store_product_reply.setDescription("商品评论表");
		tables.add(yx_store_product_reply);

		TableView yx_store_seckill = new TableView();
		yx_store_seckill.setTableName("yx_store_seckill");
		yx_store_seckill.setAlias("seckill");
		yx_store_seckill.setDescription("商品秒杀产品表");
		tables.add(yx_store_seckill);

		TableView yx_system_attachment = new TableView();
		yx_system_attachment.setTableName("yx_system_attachment");
		yx_system_attachment.setAlias("attachment");
		yx_system_attachment.setDescription("附件管理表");
		tables.add(yx_system_attachment);

		TableView yx_system_config = new TableView();
		yx_system_config.setTableName("yx_system_config");
		yx_system_config.setAlias("config");
		yx_system_config.setDescription("配置管理表");
		tables.add(yx_system_config);

		TableView yx_system_group_data = new TableView();
		yx_system_group_data.setTableName("yx_system_group_data");
		yx_system_group_data.setAlias("groupData");
		yx_system_group_data.setDescription("组合数据详情表");
		tables.add(yx_system_group_data);

		TableView yx_system_store = new TableView();
		yx_system_store.setTableName("yx_system_store");
		yx_system_store.setAlias("store");
		yx_system_store.setDescription("门店自提");
		tables.add(yx_system_store);

		TableView yx_system_user_level = new TableView();
		yx_system_user_level.setTableName("yx_system_user_level");
		yx_system_user_level.setAlias("userLevel");
		yx_system_user_level.setDescription("设置用户等级表");
		tables.add(yx_system_user_level);

		TableView yx_system_user_task = new TableView();
		yx_system_user_task.setTableName("yx_system_user_task");
		yx_system_user_task.setAlias("userTask");
		yx_system_user_task.setDescription("等级任务设置");
		tables.add(yx_system_user_task);

		TableView yx_user = new TableView();
		yx_user.setTableName("yx_user");
		yx_user.setAlias("user");
		yx_user.setDescription("用户表");
		tables.add(yx_user);

		TableView yx_user_address = new TableView();
		yx_user_address.setTableName("yx_user_address");
		yx_user_address.setAlias("userAddress");
		yx_user_address.setDescription("用户地址表");
		tables.add(yx_user_address);

		TableView yx_user_bill = new TableView();
		yx_user_bill.setTableName("yx_user_bill");
		yx_user_bill.setAlias("userBill");
		yx_user_bill.setDescription("用户账单表");
		tables.add(yx_user_bill);

		TableView yx_user_sign = new TableView();
		yx_user_sign.setTableName("yx_user_sign");
		yx_user_sign.setAlias("userSign");
		yx_user_sign.setDescription("用户签到表");
		tables.add(yx_user_sign);

		TableView yx_user_task_finish = new TableView();
		yx_user_task_finish.setTableName("yx_user_task_finish");
		yx_user_task_finish.setAlias("userTaskFinish");
		yx_user_task_finish.setDescription("用户任务完成记录表");
		tables.add(yx_user_task_finish);

		TableView yx_wechat_user = new TableView();
		yx_wechat_user.setTableName("yx_wechat_user");
		yx_wechat_user.setAlias("wechatUser");
		yx_wechat_user.setDescription("微信用户表");
		tables.add(yx_wechat_user);

		for (TableView table : tables) {

			String tableName = table.getTableName();

			List<String> names = DatabaseUtil.getColumnNames(tableName);
			List<String> types = DatabaseUtil.getColumnTypes(tableName);
			List<String> descriptions = DatabaseUtil.getColumnComments(tableName);

			TableInfo info = new TableInfo();
			info.setTableName(tableName);
			info.setModular("mall");
			info.setPermPrefix("mall");

			info.setQueryModel(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table.getAlias())+ "Query");
			info.setDaoPackage("com.alikill");
			info.setServicePackage("com.alikill.mall.service");
			info.setActionPackage("com.alikill.admin.controller.mall");
			info.setToolsPackage("com.alikill.mall.tools");
			info.setQueryPackage("com.alikill.mall.query");

			info.setRootRouter("api");
			info.setAlias(table.getAlias());
			info.setDescription(table.getDescription());
			info.setMapper(info.getAlias());

			info.setMenuName(tableName.trim().toUpperCase());
			List<Column> columns = Lists.newArrayList();
			List<Column> forms = Lists.newArrayList();
			List<Column> searchs = Lists.newArrayList();
			List<Column> tableColums = Lists.newArrayList();

			Set<String> formList = table.getForms();
			Set<String> searchList = table.getSearchs();
			Set<String> ingoreList = table.getIngores();

			if (CollectionUtils.isEmpty(formList)) {
				formList = Sets.newHashSet();
			}
			if (CollectionUtils.isEmpty(searchList)) {
				searchList = Sets.newHashSet();
			}
			for (int index = 0; index < names.size(); index++) {

				String name = names.get(index);
				String type = types.get(index);
				String description = descriptions.get(index);

				Column column = new Column();
				column.setName(name);
				column.setDescription(description);
				column.setJdbcType(type);

				columns.add(column);
//				if (formList.contains(name)) {
//					forms.add(column);
//				}
//				if (searchList.contains(name)) {
//					searchs.add(column);
//				}
//				if (!ingoreList.contains(name)) {
//					tableColums.add(column);
//				}
				info.setTables(tableColums);
				info.setColumns(columns);
				info.setSearchs(searchs);
				info.setForms(forms);
			}

			/** VUE */
//			createFile(vueTpl, info, "vue", (info.getAlias().trim().toLowerCase() + ".vue"));

			/** JS */
//			createFile(jsTpl, info, "js", (info.getAlias().trim().toLowerCase() + ".js"));

			/** Controller */
			String acName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(acTpl, info, "controller", (acName + "Controller.java"));

			/** IService */
			String svName = "I" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(svTpl, info, "service/" + info.getPermPrefix(), (svName + "SV.java"));

			/** ServiceImpl */
			String svImplName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(svImplTpl, info, "service/" + info.getPermPrefix() + "/impl", (svImplName + "SVImpl.java"));

			/** toolTpl */
			String toolName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(toolTpl, info, info.getPermPrefix() + "/tools", (toolName + "Tools.java"));

			/** queryTpl */
			String queryName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getQueryModel());
			createFile(queryTpl, info, info.getPermPrefix() + "/query", (queryName + ".java"));

		}
	}

	private static void createFile(Template tpl, TableInfo info, String path, String fileName)
			throws IOException, TemplateException {
		String dirPath = "D:/auto/" + path + "/";
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File jsfile = new File(dirPath, fileName);
		Writer writer = new FileWriter(jsfile);
		tpl.process(info, writer);
		writer.close();

		LOGGER.info(fileName + " 文件创建完成");
	}
}
